package com.juc.scheduledExecutorService.scheduledExecutorService1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class Run1 {

	//使用Callable接口进行任务延迟运行的实验，具有返回值功能
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		List<Callable> callableList = new ArrayList<Callable>();
		callableList.add(new MyCallableA());
		callableList.add(new MyCallableB());
		//调用newSingleThreadScheduledExecutor
		//取得一个单任务的计划任务执行池
		ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
		ScheduledFuture<String> futureA = executorService.schedule(callableList.get(0), 4L, TimeUnit.SECONDS);
		ScheduledFuture<String> futureB =executorService.schedule(callableList.get(1), 4L, TimeUnit.SECONDS);
		System.out.println("   X " + System.currentTimeMillis());
		System.out.println("返回值A ：" + futureA.get());
		System.out.println("返回值B ：" + futureB.get());
		System.out.println("   Y " + System.currentTimeMillis());
	}
}
